#include "stm32f10x_gpio.h"
#include "card_app.h"
#include "type_a.h"
#include "type_b.h"
#include "stdio.h"
#include "io_app.h"
#include "wiegand.h"

uint8_t Csn[10];
uint8_t Csn_len;

struct s_CardLIFO CARDLIFO[CARD_TOTAL];

static uint8_t TypeB_ReadCard(uint8_t *csn,uint8_t *csn_len);
static uint8_t TypeA_ReadCard(uint8_t *csn,uint8_t *csn_len);

uint8_t CardLIFO_Input(uint8_t *csn,uint8_t csn_len)
{
	uint8_t i;

	
	for(i=0; i<CARD_TOTAL; i++)
	{
		if(CARDLIFO[i].valid==1)
		{
			if((CARDLIFO[i].cn[0] == *csn) && (CARDLIFO[i].cn[1] == *(csn+1)) &&(CARDLIFO[i].cn[2] == *(csn+2)) &&(CARDLIFO[i].cn[3] == *(csn+3)))
				return 0;
		}
		
		if(CARDLIFO[i].valid==0)
		{
			CARDLIFO[i].cn[0] = *csn;
			CARDLIFO[i].cn[1] = *(csn+1);
			CARDLIFO[i].cn[2] = *(csn+2);
			CARDLIFO[i].cn[3] = *(csn+3);
			CARDLIFO[i].valid = 1;
			return 1;
		}
	}
	return 0;
}

void Card_Write(void)
{
	CardLIFO_Input(Csn,Csn_len);
	wiegand_input(Csn);
}

#define TYPE_A_RF_DELY		2
#define TYPE_B_RF_DELY		2
enum{
S_TypeA_RF_Dely = 0 ,
S_TypeA_RF_Open =1 ,
S_TypeA_ReadCard =2 ,
S_TypeB_RF_Open =3 ,
S_TypeB_RF_Dely =4,
S_TypeB_ReadCard =5
};

struct s_CardRead_Machine M_CardRead;

uint8_t Test_CardB_Read = 0;

void FM17550_Read_Card(void)
{
	if(M_CardRead.State == S_TypeA_ReadCard || M_CardRead.State == S_TypeB_ReadCard)
	{
		if(M_CardRead.dely != 0)
		{
			M_CardRead.dely--;
			return;
		}
		else
		{
			M_CardRead.Active = 1;
			if(M_CardRead.State == S_TypeA_ReadCard)
			{
				M_CardRead.State = S_TypeA_RF_Open;
			}
			else
			{
				M_CardRead.State = S_TypeB_RF_Open;
			}
		}
	}

	switch(M_CardRead.State)
	{
		case S_TypeA_RF_Open:
		{
			if(M_CardRead.dely)
				break;
			
			TypeA_RF_Open();
			M_CardRead.State = S_TypeA_RF_Dely;
			M_CardRead.dely = TYPE_A_RF_DELY;				//10~20MS (??????,???-1,?????????-1,??10MS->1 + 2)
		}
		break;
		case S_TypeA_RF_Dely:
		{
			if(M_CardRead.dely != 0)
				break;
		}	
		case S_TypeA_ReadCard:
		{			
			if(TypeA_Card_Detect(Csn))								//??A???
			{
				printf("Card_Yea_A");
				TypeB_RF_Close();							//RF Close ????
				M_CardRead.State = S_TypeA_ReadCard;	//????A???A???
				M_CardRead.dely = 4;
				break;
			}
			else
			{
				printf("Card_No_A");
				M_CardRead.State = S_TypeB_RF_Open;
				TypeB_RF_Close();							//RF Close ????	
				M_CardRead.dely = 4;				
			}
			M_CardRead.Active = 0;
		}
		case S_TypeB_RF_Open:
		{
			if(M_CardRead.dely)
				break;
			
			TypeB_RF_Open();
			M_CardRead.State = S_TypeB_RF_Dely;
			M_CardRead.dely = TYPE_B_RF_DELY;							//20~30MS (??????,???-1,?????????-1,??20MS->2 + 2)
		}
		break;
		case S_TypeB_RF_Dely:
		{
			if(M_CardRead.dely != 0)
				break;
		}
		case S_TypeB_ReadCard:
		{
			if(ReadCard_TypeBActive(Csn,&Csn_len))
			{
				printf("Card_Yes_B");
				M_CardRead.State = S_TypeB_ReadCard;
				TypeB_RF_Close();							//RF Close ????
				M_CardRead.dely = 4;
			}
			else
			{
				printf("Card_No_B ");
				M_CardRead.Active = 0;
				M_CardRead.State = S_TypeA_RF_Open;
				TypeB_RF_Close();							//RF Close ????	
				M_CardRead.dely = 4;				
			}
		}
		break;
		default:
			M_CardRead.State = S_TypeA_RF_Open;
			break;
	}

	if(M_CardRead.State == S_TypeA_ReadCard || M_CardRead.State == S_TypeB_ReadCard)
	{
		if(M_CardRead.Active != 1)
		{
			Card_Write();
			Led_State.color = Led_Task_Read;
			Led_State.time = 30;
			Buzzer_Suc_Read();
		}
	}
	
	if(M_CardRead.dely != 0)
		M_CardRead.dely--;
}


uint8_t Card_Read(void)
{
	if(TypeA_ReadCard(Csn,&Csn_len) == 0)
	{
		return 1;
	}
	if(TypeB_ReadCard(Csn,&Csn_len) == 0)
	{	
		return 1;
	}

	return 0;
}
uint8_t card_active_value = 0;

uint8_t Card_Is_Active(void)
{
	return card_active_value;
}

void Card_En_Active(void)
{
	card_active_value = 1;
}

void Card_Un_Active(void)
{
	card_active_value = 0;
}

uint8_t TypeB_ReadCard(uint8_t *csn,uint8_t *csn_len)
{
	if(ReadCard_TypeBActive(Csn,&Csn_len))
	{
		return 0;
	}

	return 1;
}

uint8_t TypeA_ReadCard(uint8_t *csn,uint8_t *csn_len)
{
	if(ReadCard_TypeAActive(Csn,&Csn_len))
	{
		return 0;
	}
	return 1;
}



